//
//  StatusPictureView.swift
//  SinaWeiBo
//
//  Created by dengchen on 15/11/15.
//  Copyright © 2015年 name. All rights reserved.
//

import SDWebImage
import SnapKit
import UIKit

private let PictureCellMargin:CGFloat = 5

private let PictureCellId = "PictureCell"

private var imageCount = 0;

class StatusPictureView: UICollectionView {
    
    var imageURLs:[NSURL]? {
        didSet {
            
            imageCount = (imageURLs?.count)!
            
            let viewSize = cacleViewSize()
            
            snp_updateConstraints { (make) -> Void in
                make.width.equalTo(viewSize.width)
                make.height.equalTo(viewSize.height)
            }
            
            reloadData()
                
            testLable.text = "\(imageCount)"
        }
        
    }
    
    private let testLable:UILabel = {
       let l = UILabel()
        l.backgroundColor = UIColor.redColor()
        l.textAlignment = NSTextAlignment.Center
        l.font = UIFont.systemFontOfSize(30)
        return l
    }()
  
    private func setupUI() {
        backgroundColor = UIColor.clearColor()
        addSubview(testLable)
        
        testLable.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
    }
    
    init() {
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = PictureCellMargin
        layout.minimumLineSpacing = PictureCellMargin
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        registerClass(PictureCell.self, forCellWithReuseIdentifier: PictureCellId)
        dataSource = self
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func cacleViewSize() ->CGSize {
        
        
        if imageCount == 0 {return CGSizeZero}
        
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        
        if imageCount == 1 {
        
        let key = imageURLs?.first?.absoluteString
            
         let image = SDImageCache.sharedImageCache().imageFromMemoryCacheForKey(key)
            
            let w = (DcScreenwidth * 0.5)
            
            var size = image.size
            var scale:CGFloat = 0.0
            if size.width > size.height {
                scale = size.height/size.width
                size = CGSize(width:w, height: w * scale)
            }else {
                scale = size.width/size.height
                size = CGSize(width:w * scale, height: w)
            }
            
            
            layout.itemSize = size
            
            return size
        }
        
        let column:CGFloat = 3.00000000000001
        let collectionWidth = DcScreenwidth - 2 * CellMargins
        let itemWidth = (collectionWidth - 2 * PictureCellMargin)/column
        
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        
        if imageCount == 4 {
            let w = itemWidth * 2.0000000000001 + PictureCellMargin
            return CGSizeMake(w, w)
        }
        //定义相对屏幕固定的图片宽和高
        /*
        1,2,3   -> 1行
        4,5,6   -> 2行
        7,8,9   -> 3行
        */
        
        let row = CGFloat((imageCount - 1)/Int(column)) + 1
        let h = row * itemWidth + (row - 1) * PictureCellMargin
        let w = column * itemWidth + (column - 1) * PictureCellMargin
        
        return CGSizeMake(w, h)
    }

}

 extension StatusPictureView:UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureCellId, forIndexPath: indexPath) as! PictureCell
        
        
        cell.imageURL = imageURLs![indexPath.item]
        
        return cell
    }
    
}


private class PictureCell:UICollectionViewCell {
    
   private var imageURL:NSURL? {
        didSet {
            
                imageView.sd_setImageWithURL(imageURL, placeholderImage: nil)
            }
        }

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    private func setupUI() {
        contentView.addSubview(imageView)

        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
    }

  private let imageView:UIImageView = {
        let iv = UIImageView()
        
        iv.contentMode = UIViewContentMode.ScaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
}